package webservices;

import java.util.List;
import dao.DAOFactory;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET; 
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import sun.security.krb5.Credentials;
import classes.Tip;
import classes.Vrsta;


@Path("/vrste")
public class VrstaResource {
	@Context
	 UriInfo uriInfo;
	 @Context
	 Request request;

	 @Path("/sve")
		@GET
		@Produces(MediaType.APPLICATION_XML)
		public GenericEntity<List<Vrsta>> vratiVrste() {
			try {
				DAOFactory.connect();
				List<Vrsta> vrste = DAOFactory.getVrstaDAO().selectSveVrste();
				DAOFactory.disconnect();
				return new GenericEntity<List<Vrsta>>(vrste){};
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	
	 @Path("/vrsta/{id}")
		@GET
		@Produces(MediaType.APPLICATION_XML)
		public Vrsta vratiVrstu(@PathParam("id")String sIDVrste) {
			try {
				int idVrste = Integer.parseInt(sIDVrste);
				DAOFactory.connect();
				Vrsta v = DAOFactory.getVrstaDAO().selectVrsta(idVrste);
				DAOFactory.disconnect();
				return v;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	 
	 @PUT
	  @Path("vrsta/nova")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String putTip(@HeaderParam("username")String username, @HeaderParam("password")String password, Vrsta v) throws Exception {
		  try{
			  DAOFactory.connect();
			  DAOFactory.getVrstaDAO().insertVrsta(v);
			  DAOFactory.disconnect();
			  return Boolean.toString(true);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	 
	 
	 @POST
	  @Path("vrsta/obrisi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String deleteTipPOST(@HeaderParam("username")String username, @HeaderParam("password")String password, String sIDVrste){
		  try{
			  int IDVrste = Integer.parseInt(sIDVrste);
			  DAOFactory.connect();
			  int i = DAOFactory.getVrstaDAO().deleteVrsta(IDVrste);
			  if (i!=1){
				  DAOFactory.disconnect();
				  return Boolean.toString(false);
			  }
			  DAOFactory.disconnect();
			  return Boolean.toString(true);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
}
